﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Sql;
using System.Text;
using ExamAnalysis.Model;
using System.Web.Security;


namespace ExamAnalysis.Core
{
    public static class RoleData
    {



        public static List<ClassInfo> GetClassOfRole(string userName, string roleName)
        {
            List<ClassInfo> ls = new List<ClassInfo>();
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();

            TeacherInfo teacher = db.TeacherInfo.Where(t => t.AccountID == userName).FirstOrDefault();
            if (teacher == null)
                return null;

            string[] roles = Roles.GetRolesForUser();
            switch (roleName)
            {
                case "admin":
                    return db.ClassInfo.ToList();
                case "school":
                    return db.ClassInfo.ToList();
                case "grade":
                    List<int> grades = db.TeacherInClass
                   .Where(tc => tc.TeacherID == teacher.TeacherID)
                   .Select(p => p.ClassInfo.EnrollYear)
                   .Distinct().ToList();
                    foreach (int grade in grades)
                    {
                        ls.AddRange(db.ClassInfo.Where(c => c.EnrollYear == grade).ToList());
                    }

                    break;

                case "course":
                    int subjectid = Convert.ToInt32(db.CourseInfo.Where(p => p.TeacherID == teacher.TeacherID).Single().SubjectID);
                    List<int> courses = db.CourseInfo.Where(p => p.SubjectID == subjectid).Select(p => p.CourseID).ToList();
                    List<int> exams = new List<int>();
                    foreach (int courseid in courses.Distinct())
                    {
                        exams.AddRange(
                            db.ExamInCourse.Where(p => p.CourseID == courseid)
                            .Select(p => p.ExamID).ToList());
                    }
                    List<int> students = new List<int>();
                    foreach (int examid in exams.Distinct())
                    {
                        students.AddRange(
                            db.StudentInExam.Where(p => p.ExamID == examid)
                            .Select(p => p.ExamID).ToList());
                    }
                    List<ClassInfo> classes = new List<ClassInfo>();
                    foreach (int studentid in students.Distinct())
                    {
                        classes.AddRange(db.StudentInfo.Where(p => p.StudentID == studentid)
                            .Select(p => p.ClassInfo).ToList());
                    }
                    db.StudentInExam.Select(p => p.StudentInfo.ClassID);
                    ls.AddRange(classes);
                    break;
                case "class":
                    ls.AddRange(db.TeacherInClass.Where(tc => tc.TeacherID == teacher.TeacherID).Select(p => p.ClassInfo));
                    break;
                case "teacher": ls.AddRange(db.StudentInExam
                    .Where(p => p.ExamInfo.ExamInCourse
                            .Where(q =>
                                q.CourseInfo.TeacherID == teacher.TeacherID)
                            .Count() > 0)
                    .Select(r => r.StudentInfo.ClassInfo));
                    break;

            }
            return ls.Distinct().ToList() ;

        }

        public static List<ExamInfo> GetExamInfoOfRole(string userName, string roleName)
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            List<ExamInfo> ls = new List<ExamInfo>();
            TeacherInfo teacher = db.TeacherInfo.Where(t => t.AccountID == userName).FirstOrDefault();
            CourseInfo couse = teacher.CourseInfo.FirstOrDefault();

            if (teacher == null)
                return null;
            int grade = db.TeacherInClass.Where(tc => tc.TeacherID == teacher.TeacherID).First().ClassInfo.EnrollYear;
            string[] roles = Roles.GetRolesForUser();
            switch (roleName)
            {
                case "admin":
                    ls.AddRange(db.ExamInfo.ToList());
                    break;
                case "school":
                    ls.AddRange(db.ExamInfo.ToList());
                    break;
                case "grade":
                    ls.AddRange(db.StudentInExam
                      .Where(p => p.StudentInfo.ClassInfo.EnrollYear == grade)
                      .Select(q => q.ExamInfo)
                      .Distinct());
                    break;
                case "course":
                    ls.AddRange(db.ExamInCourse.Where(ec => couse.CourseID == ec.CourseInfo.CourseID).Select(ec => ec.ExamInfo));

                    break;
                case "class":
                    //class=>exam
                    var classid = teacher.TeacherInClass.Select(p => p.ClassID);
                    List<StudentInfo> studentIds = new List<StudentInfo>();
                    List<StudentInfo> listStuInfo = new List<StudentInfo>();
                    foreach (int i in classid)
                    {
                        listStuInfo = db.ClassInfo.Where(p => p.ClassID == i).First().StudentInfo.ToList();
                        studentIds.AddRange(listStuInfo.ToArray());
                    }
                    List<int> sidls = studentIds.Select(s => s.StudentID).Distinct().ToList();
                    foreach (int j in sidls)
                    {
                        ls.AddRange(db.StudentInExam.Where(s => s.StudentID == j).Select(p => p.ExamInfo));

                    }
                    break;
                case "teacher":
                    int courseid = Convert.ToInt32(db.CourseInfo.Where(s => s.TeacherID == teacher.TeacherID).Select(p => p.SubjectID).FirstOrDefault());
                    ls.AddRange(db.ExamInCourse.Where(s => s.CourseID == courseid).Select(p => p.ExamInfo));

                    break;

            }


            return ls.Distinct().ToList();
        }
        public static List<ExamInfo> GetExamInfoByUserName(string userName)
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            List<ExamInfo> ls = new List<ExamInfo>();
            TeacherInfo teacher = db.TeacherInfo.Where(t => t.AccountID == userName).FirstOrDefault();
            CourseInfo couse = teacher.CourseInfo.FirstOrDefault();

            if (teacher == null)
                return null;
            int grade = db.TeacherInClass.Where(tc => tc.TeacherID == teacher.TeacherID).First().ClassInfo.EnrollYear;
            string[] roles = Roles.GetRolesForUser();
            if (roles.Contains("admin"))
                ls.AddRange(db.ExamInfo.ToList());
            if (roles.Contains("school"))
                ls.AddRange(db.ExamInfo.ToList());
            if (roles.Contains("grade"))
            {

                ls.AddRange(db.StudentInExam
                       .Where(p => p.StudentInfo.ClassInfo.EnrollYear == grade)
                       .Select(q => q.ExamInfo)
                       .Distinct());
            }

            if (roles.Contains("course"))
                ls.AddRange(db.ExamInCourse.Where(ec => couse.CourseID == ec.CourseInfo.CourseID).Select(ec => ec.ExamInfo));
            if (roles.Contains("class"))
            {
                //class=>exam
                var classid = teacher.TeacherInClass.Select(p => p.ClassID);
                List<StudentInfo> studentIds = new List<StudentInfo>();
                List<StudentInfo> listStuInfo = new List<StudentInfo>();
                foreach (int i in classid)
                {
                    listStuInfo = db.ClassInfo.Where(p => p.ClassID == i).First().StudentInfo.ToList();
                    studentIds.AddRange(listStuInfo.ToArray());
                }
                List<int> sidls = studentIds.Select(s => s.StudentID).Distinct().ToList();
                foreach (int j in sidls)
                {
                    ls.AddRange(db.StudentInExam.Where(s => s.StudentID == j).Select(p => p.ExamInfo));

                }

            }
            if (roles.Contains("teacher"))
            //teacher=>exam
            {
                int courseid = Convert.ToInt32(db.CourseInfo.Where(s => s.TeacherID == teacher.TeacherID).Select(p => p.SubjectID).FirstOrDefault());
                ls.AddRange(db.ExamInCourse.Where(s => s.CourseID == courseid).Select(p => p.ExamInfo));
            }

            return ls.Distinct().ToList();
        
        
        
        
        
        
        
        }



        public static List<ClassInfo> GetClassByUserName(string userName)
        {
            List<ClassInfo> ls = new List<ClassInfo>();
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();

            TeacherInfo teacher = db.TeacherInfo.Where(t => t.AccountID == userName).FirstOrDefault();
            if (teacher == null)
                return null;

            string[] roles = Roles.GetRolesForUser();
            if (roles.Contains("admin"))
                ls.AddRange(db.ClassInfo.ToList());
            if (roles.Contains("school"))
                ls.AddRange(db.ClassInfo.ToList());
            if (roles.Contains("grade"))
            {
                List<int> grades = db.TeacherInClass
                    .Where(tc => tc.TeacherID == teacher.TeacherID)
                    .Select(p => p.ClassInfo.EnrollYear)
                    .Distinct().ToList();
                foreach (int grade in grades)
                {
                    ls.AddRange(db.ClassInfo.Where(c => c.EnrollYear == grade).ToList());
                }
            }
            if (roles.Contains("course"))
            {
                int subjectid = Convert.ToInt32(db.CourseInfo.Where(p => p.TeacherID == teacher.TeacherID).Single().SubjectID);
                List<int> courses = db.CourseInfo.Where(p => p.SubjectID == subjectid).Select(p => p.CourseID).ToList();
                List<int> exams = new List<int>();
                foreach (int courseid in courses.Distinct())
                {
                    exams.AddRange(
                        db.ExamInCourse.Where(p => p.CourseID == courseid)
                        .Select(p => p.ExamID).ToList());
                }
                List<int> students = new List<int>();
                foreach (int examid in exams.Distinct())
                {
                    students.AddRange(
                        db.StudentInExam.Where(p => p.ExamID == examid)
                        .Select(p => p.ExamID).ToList());
                }
                List<ClassInfo> classes = new List<ClassInfo>();
                foreach (int studentid in students.Distinct())
                {
                    classes.AddRange(db.StudentInfo.Where(p => p.StudentID == studentid)
                        .Select(p => p.ClassInfo).ToList());
                }
                db.StudentInExam.Select(p => p.StudentInfo.ClassID);
                ls.AddRange(classes);
            }

            if (roles.Contains("class"))
                ls.AddRange(db.TeacherInClass.Where(tc => tc.TeacherID == teacher.TeacherID).Select(p => p.ClassInfo));
            if (roles.Contains("teacher"))
                ls.AddRange(db.StudentInExam
                    .Where(p => p.ExamInfo.ExamInCourse
                            .Where(q =>
                                q.CourseInfo.TeacherID == teacher.TeacherID)
                            .Count() > 0)
                    .Select(r => r.StudentInfo.ClassInfo));

            return ls.Distinct().ToList();
        }


    }
}
